package com.jchao.auth.utils;

import com.jchao.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * create 2023/11/14 16:08
 */


public class MenuHelper {


    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        ArrayList<SysMenu> sysMenus = new ArrayList<>();
        for (SysMenu menu : sysMenuList) {
            if (menu.getParentId().intValue() == 0) {
                sysMenus.add(findChildMenus(menu, sysMenuList));
            }
        }
        return sysMenus;
    }

    private static SysMenu findChildMenus(SysMenu menu, List<SysMenu> sysMenuList) {
        ArrayList<SysMenu> childMenus = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getParentId().equals(menu.getId())) {
                childMenus.add(findChildMenus(sysMenu, sysMenuList));
            }
        }
        menu.setChildren(childMenus);
        return menu;
    }
}
